package com.luo.utils;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;
import android.widget.RemoteViews;

import com.luo.demo.R;

import java.util.List;

/**
 * Created by Administrator on 2017/5/22.
 */

public class NotifyUtil {

    public static void notifyActivity(Context context, Intent intent, String title, String ticker, String content, int icon){
        PendingIntent pendingIntent = PendingIntent.getActivity(context,1,intent,0);
        Notification.Builder nb = new Notification.Builder(context)
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .setPriority(Notification.PRIORITY_HIGH)//设置前端展示
                .setLights(0xff00ff00,300,1000)//该方法设置了显示lights
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)//设置震动声音灯
                .setTicker(ticker)//设置展示，5.0以上无效，需要开通辅助功能
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1){
            nb.setShowWhen(true);//sdk 17 以上需要设置该选项才能显示时间
        }
        Notification notification = nb.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;//设置不能手动清除和全部清除
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1,notification);
    }

    public static void notifyReceiver(Context context, Intent intent, String title, String ticker, String content, int icon){
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1,intent,0);
        Notification.Builder nb = new Notification.Builder(context)
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .setPriority(Notification.PRIORITY_HIGH)//设置前端展示
                .setLights(0xff00ff00,300,1000)//该方法设置了显示lights
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)//设置震动声音灯
                .setTicker(ticker)//设置展示，5.0以上无效，需要开通辅助功能
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1){
            nb.setShowWhen(true);//sdk 17 以上需要设置该选项才能显示时间
        }
        Notification notification = nb.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;//设置不能手动清除和全部清除
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1,notification);
    }

    public static void notify(Context context, Intent intent,String title, String ticker, String content, int icon){
        PendingIntent pendingIntent = PendingIntent.getActivity(context,1,intent,0);
        Notification.Builder builder = new Notification.Builder(context);
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification);
        remoteView.setTextViewText(R.id.notification_title, title);
        remoteView.setTextViewText(R.id.notification_text, content);
        remoteView.setImageViewBitmap(R.id.notification_large_icon, BitmapFactory.decodeResource(context.getResources(),R.mipmap.a3));
//        remoteView.setImageViewResource(R.id.notification_large_icon,R.mipmap.a3);
        remoteView.setImageViewResource(R.id.notification_small_icon, icon);
        builder.setContent(remoteView);
    }

    /**
     * 判断应用是否在前端界面
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isTopTask(Context context ,String packageName){
        packageName = TextUtils.isEmpty(packageName) ? context.getPackageName() : packageName;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> appTasks = activityManager.getRunningTasks(1);
        if (appTasks == null || appTasks.size() == 0){
            return false;
        }
        return appTasks.get(0).topActivity.getPackageName().equals(packageName);
    }

    /**
     * 获取packageName应用所在的taskid
     * @param context
     * @param packageName
     * @return
     */
    public static int getTaskID(Context context,String packageName){
        int taskID = -1;
        packageName = TextUtils.isEmpty(packageName) ? context.getPackageName() : packageName;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> appTasks = activityManager.getRunningTasks(50);
        if (appTasks != null){
            ComponentName  componentName;
            for (ActivityManager.RunningTaskInfo appTask : appTasks) {
                componentName = appTask.baseActivity;
                if (componentName.getPackageName().equals(packageName)){
                    taskID = appTask.id;
                    break;
                }
            }
        }
        return taskID;
    }

    /**
     * 获取packageName应用的正在运行的栈顶activity
     * @param context
     * @param packageName
     * @return
     */
    public static ComponentName getTopActivity(Context context, String packageName){
        packageName = packageName == null ? context.getPackageName() : packageName;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> appTasks = activityManager.getRunningTasks(50);
        if (appTasks != null){
            ComponentName  componentName;
            for (ActivityManager.RunningTaskInfo appTask : appTasks) {
                componentName = appTask.topActivity;
                if (componentName.getPackageName().equals(packageName)){
                    return componentName;
                }
            }
        }
        return null;
    }

    /**
     * 判断packageName的应用是否在运行中，如果packageName为空，则默认判断context所属的应用是否在运行中
     * @param context
     * @return
     */
    public static boolean isRunning(Context context,String packageName){
        packageName = packageName == null ? context.getPackageName() : packageName;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> appTasks = activityManager.getRunningTasks(50);
        if (appTasks != null){
            for (ActivityManager.RunningTaskInfo appTask : appTasks) {
                if (appTask.baseActivity.getPackageName().equals(packageName)){
                    return true;
                }
            }
        }
        return false;
    }
}
